 /************************************************************************************************************
 * @attention
 *
 * Firmware Disclaimer Information
 *
 * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
 *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
 *    other intellectual property laws.
 *
 * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
 *    other than HOLTEK and the customer.
 *
 * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
 *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
 *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
 *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
 *
 * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
 ************************************************************************************************************/
//Generated by HT32CodeConfig V1.1.3 Build 240823
//-----------------------------------------------------------------------------
#include "SPI1.h"

//-----------------------------------------------------------------------------
void SPI1_Configuration(void)
{
  SPI_InitTypeDef SPI_InitStruct;
  CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
  CKCUClock.Bit.SPI1   = 1;
  CKCUClock.Bit.AFIO   = 1;
  CKCU_PeripClockConfig(CKCUClock, ENABLE);

  AFIO_GPxConfig(GPIO_PA, AFIO_PIN_0, AFIO_FUN_SPI);  // Config AFIO mode
  AFIO_GPxConfig(GPIO_PA, AFIO_PIN_1, AFIO_FUN_SPI);  // Config AFIO mode

  SPI_InitStruct.SPI_Mode = SPI_MASTER;
  SPI_InitStruct.SPI_DataLength = SPI_DATALENGTH_8;
  SPI_InitStruct.SPI_SELMode = SPI_SEL_SOFTWARE;
  SPI_InitStruct.SPI_SELPolarity = SPI_SELPOLARITY_LOW;
  SPI_InitStruct.SPI_FirstBit = SPI_FIRSTBIT_MSB;
  SPI_InitStruct.SPI_CPOL = SPI_CPOL_LOW;
  SPI_InitStruct.SPI_CPHA = SPI_CPHA_FIRST;
  SPI_InitStruct.SPI_ClockPrescaler = 4;
  SPI_InitStruct.SPI_FIFO = SPI_FIFO_ENABLE;
  SPI_InitStruct.SPI_RxFIFOTriggerLevel = 4;
  SPI_InitStruct.SPI_TxFIFOTriggerLevel = 4;
  SPI_Init(HT_SPI1, &SPI_InitStruct);
  SPI_SELOutputCmd(HT_SPI1, ENABLE);
  SPI_Cmd(HT_SPI1, ENABLE);
}

//-----------------------------------------------------------------------------
u16 SPI1_MASTER_SendByte(u8 byte)
{
  while (SPI_GetFIFOStatus(HT_SPI1, SPI_FIFO_TX) != 0);
  while (SPI_GetFIFOStatus(HT_SPI1, SPI_FIFO_RX) != 0)
  {
    SPI_ReceiveData(HT_SPI1);
  }
  SPI_SendData(HT_SPI1,  byte);
  while (SPI_GetFIFOStatus(HT_SPI1, SPI_FIFO_RX) == 0);
  return SPI_ReceiveData(HT_SPI1);
}

//-----------------------------------------------------------------------------
void SPI1_MASTER_Read(u8* ReadBuffer, u16 len)
{
  SPI1_MASTER_SEL_ACTIVE();
  while(len--)
  {
    *ReadBuffer++ = SPI1_MASTER_SendByte(0xFF);
  }
  SPI1_MASTER_SEL_INACTIVE();
}

//-----------------------------------------------------------------------------
void SPI1_MASTER_Write(u8* WriteBuffer, u16 len)
{
  SPI1_MASTER_SEL_ACTIVE();
  while(len--)
  {
    SPI1_MASTER_SendByte(*WriteBuffer++);
  }
  SPI1_MASTER_SEL_INACTIVE();
}

//-----------------------------------------------------------------------------
static u8 test_array[8];
void SPI1_test(void)
{
  u8 i;
  for(i=0; i<8; i++)
  {
    test_array[i] = i;
  }
  SPI1_MASTER_Write(test_array, 8);
}
//-----------------------------------------------------------------------------

void SPI_WriteByte(uint8_t *TxData,uint16_t size)
{
	 SPI1_MASTER_Write (TxData,size);
}
